//
//  LoadNavViewController.m
//  基本框架
//
//  Created by runone02 on 2018/8/7.
//  Copyright © 2018年 liqiyun. All rights reserved.
//

#import "LoadNavViewController.h"

#define headImgWidth 55

@interface LoadNavViewController ()<UITableViewDelegate,UITableViewDataSource,UIScrollViewDelegate>
/** */
@property (nonatomic, strong) UITableView *mainTableView;
@property (nonatomic, strong) UIImageView *HeadImageView;

@property (nonatomic, assign) CGFloat lastOffsetY;
@property (nonatomic, strong) UIImageView *navImageView;

@property (nonatomic, strong) UIImageView *headImg;
@property (nonatomic, strong) UILabel *nameLable;
@property (nonatomic, strong) UIImageView *smallImgView;

@property (nonatomic, assign) CGFloat needMoveY;//Y方向可移动的距离
@property (nonatomic, assign) CGFloat MoveMultiple;//Y方向每移动一个像素，headImg的X方向需移动的倍数
@property (nonatomic, assign) CGFloat widthMultiple;//头像的宽度倍数
@end

@implementation LoadNavViewController

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = YES;
}
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    self.navigationController.navigationBar.hidden = NO;
}



- (void)viewDidLoad {
    [super viewDidLoad];
    
   self.mainTableView = [[UITableView alloc]initWithFrame:CGRectMake(0,0, SCREEN_WIDTH, SCREEN_HEIGHT) style:UITableViewStylePlain];
    self.mainTableView.backgroundColor = [UIColor whiteColor];
    self.mainTableView.delegate = self;
    self.mainTableView.dataSource = self;
    
    // 设置回调（一旦进入刷新状态，就调用target的action，也就是调用self的loadNewData方法）
    MJRefreshGifHeader *header = [MJRefreshGifHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadData)];
    NSArray *Animation_ImageArray = [NSArray arrayWithObjects:IMAGE(@"Animation_1"), IMAGE(@"Animation_2"),IMAGE(@"Animation_3"), IMAGE(@"Animation_2"), nil];
    [header setImages:Animation_ImageArray forState:MJRefreshStateRefreshing];
    // 设置header
    self.mainTableView.mj_header = header;
    [self.view addSubview:self.mainTableView];
    
    
    //tableView的headView
    self.HeadImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 145)];
    self.HeadImageView.image = IMAGE(@"headBg");
    //头像
    self.headImg = [[UIImageView alloc]initWithFrame:CGRectMake(25,self.HeadImageView.height-13-headImgWidth, headImgWidth,headImgWidth)];
    self.headImg.image = IMAGE(@"gestureHead");
    [self.HeadImageView addSubview:self.headImg];
    
    //登陆、注册
    self.nameLable = [[UILabel alloc]initWithFrame:CGRectMake(self.headImg.max_X+30, self.headImg.y, self.headImg.width, self.headImg.height)];
    self.nameLable.text = @"登录 | 注册                                >";
    self.nameLable.textColor = [UIColor whiteColor];
    self.nameLable.font = [UIFont systemFontOfSize:18];
    [self.nameLable sizeToFit];
    self.nameLable.centerY = self.headImg.centerY;
    [self.HeadImageView addSubview:self.nameLable];
    
    [self createNavView];
    self.mainTableView.tableHeaderView = self.HeadImageView;
}

#pragma mark - createView
- (void)createNavView{
    //虚拟导航栏，存放title，设置，客服，信息btn
    self.navImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0,self.view.width, 64)];
    [self.view addSubview:self.navImageView];
    //title
    UILabel *titleLable = [[UILabel alloc]initWithFrame:CGRectMake(0, 20, 30, 44)];
    titleLable.centerX = self.navImageView.centerX;
    titleLable.backgroundColor = [UIColor clearColor];
    titleLable.text = @"我的";
    titleLable.textColor = [UIColor whiteColor];
    titleLable.textAlignment = NSTextAlignmentCenter;
    titleLable.font = [UIFont systemFontOfSize:16];
    [titleLable sizeToFit];
    [self.navImageView addSubview:titleLable];
    //设置
    UIButton *setBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 20, 44, 44)];
    titleLable.centerY = setBtn.centerY;
    [setBtn setImage:IMAGE(@"blueSetting") forState:UIControlStateNormal];
    [self.navImageView addSubview:setBtn];
    
    //消息
    UIButton *newsBtn = [[UIButton alloc]initWithFrame:CGRectMake(self.navImageView.width-setBtn.width, setBtn.y, setBtn.width, setBtn.height)];
    [newsBtn setImage:IMAGE(@"blueMessage") forState:UIControlStateNormal];
    [self.navImageView addSubview:newsBtn];
    //客服
    UIButton *askBtn = [[UIButton alloc]initWithFrame:CGRectMake(newsBtn.x-newsBtn.width, newsBtn.y, newsBtn.width, newsBtn.height)];
    [askBtn setImage:IMAGE(@"blueOnLine") forState:UIControlStateNormal];
    [self.navImageView addSubview:askBtn];
    
    //小headImg
    self.smallImgView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 25, 25)];
    self.smallImgView.centerY = titleLable.centerY - 3;
    self.smallImgView.x = titleLable.x - 10- 30;
    [self.navImageView addSubview:self.smallImgView];
    
    //    创建完所有的东西之后计算需要移动的位置和距离
    [self dealPosition];
}

- (void)dealPosition{
    //头像距离左端的距离不能直接用offset来决定，因为屏幕宽度不一样，要根据屏幕宽度来计算出一个倍数
    //1、计算出总共需要向右移动多少距离
    CGFloat needMoveX = self.smallImgView.x - self.headImg.x;
    //2、找出上下移动多少距离就得到位
    self.needMoveY = self.HeadImageView.height - self.navImageView.height;
    //3、根据这两个参数，计算出倍数，然后重新定义leftPadding
    self.MoveMultiple = self.needMoveY/needMoveX;
    
    //    1、首先确定初始大小和末尾大小的差
    //    初始是headImgWidth，末尾是25
    //    2、然后是Y移动的距离
    //    3、根据移动的倍数计算应该减少多少
    CGFloat subWidth = self.headImg.width - self.smallImgView.width;
    self.widthMultiple = (CGFloat)subWidth / self.needMoveY;
}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    CGFloat offsetY = scrollView.contentOffset.y;//计算当前偏移位置
    self.navImageView.alpha = 1;
    
    CGFloat leftPadding = offsetY/self.MoveMultiple + 25;//头像距左端原有25像素；moveX = moveY/倍数;将x设置给headImg；
    CGFloat headWidth = headImgWidth - offsetY*self.widthMultiple;//计算头像移动过程中的宽度
    
    if (offsetY > self.needMoveY) {//和nav重合的状态，直接显示navImageView
        //        1、设置navImageView的背景图片；小头像出现
        self.navImageView.image = IMAGE(@"mineNavBar");
        self.smallImgView.image = IMAGE(@"gestureHead");
        
    }else if (offsetY <= 0) {//说明是下拉，隐藏 navView
        CGFloat imgAlpha = 1 + offsetY/40;//下拉40个像素就完全隐藏
        self.navImageView.alpha = imgAlpha;
        self.headImg.frame = CGRectMake(25, self.HeadImageView.height-13-headImgWidth, headImgWidth, headImgWidth);
        self.nameLable.alpha = 1;
    }else{//移动过程
        self.navImageView.image = IMAGE(@"");
        self.smallImgView.image = IMAGE(@"");
        self.headImg.frame = CGRectMake(leftPadding, self.HeadImageView.height-13-headWidth, headWidth, headWidth);
        self.nameLable.x = self.headImg.max_X + 30;
        self.nameLable.alpha = 1 - offsetY/self.needMoveY;
    }
}


#pragma mark -tablviewDelegate
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 20;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *cellID = [NSString stringWithFormat:@"%ld%ld",(long)indexPath.section,(long)indexPath.row];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
        cell.textLabel.text = [NSString stringWithFormat:@"indexPath: %ld",(long)indexPath.row];
        
    }
    return cell;
}

#pragma mark -BtnClicked

- (void)loadData{//    下拉刷新
    self.navImageView.alpha = 0;
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.mainTableView.mj_header endRefreshing];
    });
}

@end
